/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doc2db.fileWorker.xml;

import doc2db.Keey;
import doc2db.table.DocTableHead;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author yuriy
 */
public class DbParam {
    private String forAll = "varchar(51)";
    private List<MyDimen> myDimen = new ArrayList<MyDimen>();
    private boolean needInHeadType = false;

    public void setClientEncoding (String s){
        if (s.equals("ul")) {
            return;
        }
        Keey.client_encoding = "" + s;
    }

    public void setHeadType(String inStr){
        if (inStr.equals("ul")) {
            return;
        }
        String arr[] = inStr.trim().split("\\|\\|");
        if(arr.length < 1){
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            String str [] = arr[i].trim().split(":=");
            if(str.length < 2){
                continue;
            }
            String s1 = "" + str[0].trim();
            String s2 = "" + str[1].trim();
            if(s1.equals("*")){
                forAll = s2;
            }else {
                myDimen.add(new MyDimen(s1, s2));
            }
        }
        needInHeadType = true;
    }

    public void setTableHeadType(DocTableHead head){
        for (int i = 0; i < Math.min(head.getSize(), myDimen.size()); i++) {
            if(myDimen.get(i).s1.equals("?")){
                head.get(i).setHead("" + myDimen.get(i).s2);
            }
        }
        for (int i = 0; i < head.getSize(); i++) {
            if(this.setCellType(head, i)){
            }else{
                head.get(i).setHead("" + forAll);
            }
        }
    }

    private boolean setCellType(DocTableHead head, int where){
        for (int i = 0; i < myDimen.size(); i++) {
            if(myDimen.get(i).s1.equals(head.get(where).getName())){
                head.get(i).setHead("" + myDimen.get(i).s2);
                return true;
            }
        }
        if(where < myDimen.size()){
            if(myDimen.get(where).s1.equals("?")){
                if(myDimen.get(where).s2.equals(head.get(where).getHead())){
                    head.get(where).setHead("" + myDimen.get(where).s2);
                    return true;
                }
            }
        }
        return false;
    }

    public boolean isNeedInHeadType() {
        return needInHeadType;
    }

    public void setNeedInHeadType(boolean needInHeadType) {
        this.needInHeadType = needInHeadType;
    }

    private class MyDimen{
        public String s1 = "";
        public String s2 = "";
        public MyDimen(){}
        public MyDimen (String __s1, String __s2){
            s1 = "" + __s1;
            s2 = "" + __s2;
        }
    }
}
